﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.MonThiService;
using eTMS.Controllers.PhongThiService;
using eTMS.Controllers.NamHocService;
using eTMS.Controllers.HocKyService;

namespace eTMS.UserControls
{
    public partial class TreeKyThiMonThi : UserControl
    {
        public TreeKyThiMonThi()
        {
            InitializeComponent();
        }
        public event EventHandler OnSelectedNodeChanged;
        public MonThiEntity GetSelectedMonThi()
        {
            if (treeView1.SelectedNode == null) return null;
            if (treeView1.SelectedNode.Tag == null) return null;
            if (!(treeView1.SelectedNode.Tag is MonThiEntity)) return null;
            return (MonThiEntity)treeView1.SelectedNode.Tag;
        }
        public PhongThiEntity GetSelectedPhongThi()
        {
            if (treeView1.SelectedNode == null) return null;
            if (treeView1.SelectedNode.Tag == null) return null;
            if (!(treeView1.SelectedNode.Tag is PhongThiEntity)) return null;
            return (PhongThiEntity)treeView1.SelectedNode.Tag;
        }


        public NamHocEntity GetSelectedNamHoc()
        {
            if (comboBoxNamHoc1.SelectedItem == null) return null;
            return (NamHocEntity)comboBoxNamHoc1.SelectedItem;
        }
        public HocKyEntity GetSelectedHocKy()
        {
            if (comboBoxHocKy1.SelectedItem==null) return null;
            return (HocKyEntity)comboBoxHocKy1.SelectedItem;
        }


        public List<PhongThiEntity> GetListSelectedPhongThi()
        {
            List<PhongThiEntity> ls = new List<PhongThiEntity>();
            foreach (TreeNode node in treeView1.SelectedNodes)
            {
                if (node.Tag is PhongThiEntity)
                {
                    ls.Add((PhongThiEntity)node.Tag);
                }
            }
            return ls;
        }
        private MonThiController controller;
        public void SetController(MonThiController monThiController)
        {
            controller = monThiController;
        }
        private bool _IsPhongThiVisible = false;
        public bool IsPhongThiVisible
        {
            get
            {
                return _IsPhongThiVisible;
            }
            set
            {
                _IsPhongThiVisible = value;
            }
        }
        public void UpdateToScreen()
        {
            BindData();
        }
        private void BindData()
        {
            if (controller == null) controller = new MonThiController();
            controller.LoadMonThiByKyThi((int)comboBoxKyThi1.SelectedValue);
            treeView1.Nodes.Clear();
            TreeNode root = new TreeNode(comboBoxKyThi1.Text);
            treeView1.Nodes.Add(root);
            root.ImageIndex = 0;
            foreach (DataRow row in controller.Table.Rows)
            {
                MonThiEntity oMonThi = controller.MapDataRowToObject(row);
                TreeNode node = new TreeNode(row["TenMonHoc"].ToString() + "-" + eTMS.Forms.KhoaStringMapping.Khoa + ":" + row["TenKhoa"].ToString());
                node.Tag = oMonThi;
                node.ImageIndex = 1;
                root.Nodes.Add(node);
                if (_IsPhongThiVisible)
                {
                    BindPhongThi(oMonThi.MonThi_ID, node);
                }
            }
            root.Expand();
        }
        private void BindPhongThi(int MonThi_ID, TreeNode node)
        {
            controller.LoadAllPhongThi(MonThi_ID);
            foreach (PhongThiEntity oPhongThi in controller.LsPhongThi)
            {
                TreeNode snode = new TreeNode(oPhongThi.MaPhongThi);
                snode.Tag = oPhongThi;
                snode.ImageIndex = 2;
                node.Nodes.Add(snode);
            }
        }
        public void UpdateCurrentMonThi()
        {
            if (treeView1.SelectedNode == null) return;
            if (controller.MonThiObject != null)
            {
                if (treeView1.SelectedNode.Tag is MonThiEntity)
                {
                    treeView1.SelectedNode.Nodes.Clear();
                    BindPhongThi(controller.MonThiObject.MonThi_ID, treeView1.SelectedNode);
                }
                else if (treeView1.SelectedNode.Tag is PhongThiEntity)
                {
                    TreeNode pNode = treeView1.SelectedNode.Parent;
                    pNode.Nodes.Clear();
                    BindPhongThi(controller.MonThiObject.MonThi_ID, pNode);
                }
            }
        }
        private void comboBoxKyThi1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBoxKyThi1.SelectedIndex >= 0)
                BindData();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (OnSelectedNodeChanged != null)
                OnSelectedNodeChanged(sender, new EventArgs());
        }

        private void TreeKyThiMonThi_Load(object sender, EventArgs e)
        {
            if (this.DesignMode) return;
            NamHocController.LoadAllNamHoc(false);
            HocKyController.LoadAllHocKy(false);
            comboBoxNamHoc1.BindData();
            comboBoxHocKy1.BindData();
        }

        private void comboBoxHocKy1_SelectedValueChanged(object sender, EventArgs e)
        {
            comboBoxKyThi1.ClearSearchValue();
            if (comboBoxHocKy1.SelectedIndex >= 0)
            {
                comboBoxKyThi1.AddSearchValues("HocKy_ID", comboBoxHocKy1.SelectedValue);
            }
            if (comboBoxNamHoc1.SelectedIndex >= 0)
            {
                comboBoxKyThi1.AddSearchValues("NamHoc_ID", comboBoxNamHoc1.SelectedValue);
            }
            KyThiController.LoadAllKyThi(false);
            treeView1.Nodes.Clear();
            comboBoxKyThi1.BindData();
        }
        public bool AllowDragDrop
        {
            get
            {
                return treeView1.AllowDrop;
            }
            set
            {
                treeView1.AllowDrop = value;
            }
        }
        public event DragEventHandler OnDragDrop;
        public event DragEventHandler OnDragEnter;
        public event DragEventHandler OnDragOver;  
        private TreeNode currentNodeDrop = null;
        private PhongThiEntity currentPhongThiDrop;
        public TreeNode GetNodeDragDrop()
        {
            return currentNodeDrop;
        }
        public PhongThiEntity GetPhongThiDragDrop()
        {
            return currentPhongThiDrop;
        }
        private void treeView1_DragDrop(object sender, DragEventArgs e)
        {
            currentNodeDrop = treeView1.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));
            currentPhongThiDrop = null;
            if (currentNodeDrop != null)
            {
                if (currentNodeDrop.Tag is PhongThiEntity)
                {
                    currentPhongThiDrop = (PhongThiEntity)currentNodeDrop.Tag;
                }
            }
            if (OnDragDrop != null)
                OnDragDrop(sender, e);            
        }

        private void treeView1_DragEnter(object sender, DragEventArgs e)
        {
            if (OnDragEnter != null)
                OnDragEnter(sender, e);       
        }

        private void treeView1_DragOver(object sender, DragEventArgs e)
        {
            if (OnDragOver != null)
                OnDragOver(sender, e); 
        }
    }
}
